package com.example.royalchef.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.royalchef.R;
import com.example.royalchef.activity.HomeDetailActivity;
import com.example.royalchef.adaper.HomeAdapter;
import com.example.royalchef.base.BaseFragment;
import com.example.royalchef.base.BaseRecyclerViewAdapter;
import com.example.royalchef.bean.SearchInfo;
import com.example.royalchef.view.DividerItemDecoration;
import com.example.royalchef.view.SearchView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * author: Administrator
 * created on: 2017/6/27 9:42
 * description:
 */

public class HomeFragment extends BaseFragment {

    private SearchView mSearchView;
    private RecyclerView mRecyclerView;
    private ArrayList<SearchInfo> searchInfos;
    private View mView;
    private LinearLayoutManager manager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getContext(), R.layout.fragment_home, null);
        initView();
        initData();
        return mView;
    }

    private void initView() {
        mSearchView = (SearchView) mView.findViewById(R.id.sv_search_cook);
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.rlv_home_list);
        manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
    }

    private void initData() {
        RequestQueue queue = Volley.newRequestQueue(getContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://192.168.12.248:8080/cookbook/list.json?page=0&limit=7",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject object = new JSONObject(response);
                            JSONArray data = object.getJSONArray("data");
                            searchInfos = new ArrayList<>();
                            for (int i = 0; i < data.length(); i++) {
                                JSONObject jsonObject = data.getJSONObject(i);
                                JSONObject user = jsonObject.getJSONObject("user");
                                String nickname = user.getString("nickname");
                                String title = jsonObject.getString("title");
                                String cover = jsonObject.getString("cover");
                                String content = jsonObject.getString("content");
                                String createdat = jsonObject.getString("createdat");
                                SearchInfo searchInfo = new SearchInfo(nickname, title, cover, content, createdat);
                                searchInfos.add(searchInfo);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        HomeAdapter adapter = new HomeAdapter(searchInfos, getActivity(), R.layout.item_home);
                        mRecyclerView.setAdapter(adapter);
                        adapter.setOnItemClickLitener(new BaseRecyclerViewAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                Intent intent = new Intent(getActivity(), HomeDetailActivity.class);
                                intent.putExtra("searchInfo", searchInfos.get(position));
                                startActivity(intent);
                            }
                        });
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        queue.add(stringRequest);

    }


}
